package com.itheima.webaitliasserver.controller;

import com.itheima.webaitliasserver.pojo.Dept;
import com.itheima.webaitliasserver.pojo.Result;
import com.itheima.webaitliasserver.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: xiaoZhang
 * @Description:
 * @Author: Zhang
 * @CreateDate: 2025-06-26 15:27
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    private DeptService deptService;
    // 拿取ioc容器中的实例
    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    @GetMapping
    public Result deptList() {
        List<Dept> deptList = deptService.queryDeptList();
        log.info("deptList: {}", deptList);
        return Result.success(deptList);
    }
    /**
    * @Author: Zhang
    * @param:
    * @createTime: 2025/6/29 10:21
    * @Description: 根据id删除 员工数据
    * @Return: 无
    */
    @DeleteMapping
    public Result deleteDeptById(Integer deptId) {
        if(deptId == null) {
            return Result.error("部门Id不能为空");
        }
        deptService.queryDeptById(deptId);
        return Result.success();
    }

    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        if(dept.getName() == null || "".equals(dept.getName())) {
            return Result.error("部门名称不能为空");
        }
        String msg = deptService.addDept(dept);
        return Result.success(msg);
    }
}
